home *** CD-ROM | disk | FTP | other *** search
/ One Click 11 / OneClick11.iso / Bancos de Dados / Conversao / Mysql2Excel / Setup.exe / Mysql2Excel.exe / MySQLdb / cursors.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-06-23  |  17.4 KB  |  404 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''MySQLdb Cursors
  5.  
  6. This module implements Cursors of various types for MySQLdb. By
  7. default, MySQLdb uses the Cursor class.
  8.  
  9. '''
  10. import re
  11. insert_values = re.compile('values\\s*(\\(.+\\))', re.IGNORECASE)
  12. from _mysql import escape, ProgrammingError, Warning
  13.  
  14. class BaseCursor:
  15.     '''A base for Cursor classes. Useful attributes:
  16.     
  17.     description -- DB API 7-tuple describing columns in last query
  18.     arraysize -- default number of rows fetchmany() will fetch
  19.     
  20.     See the MySQL docs for more information.'''
  21.     
  22.     def __init__(self, connection):
  23.         self._BaseCursor__conn = connection
  24.         self.description = None
  25.         self.rowcount = -1
  26.         self.arraysize = 100
  27.         self._executed = None
  28.  
  29.     
  30.     def __del__(self):
  31.         self.close()
  32.  
  33.     
  34.     def close(self):
  35.         '''Close the cursor. No further queries will be possible.'''
  36.         if not (self._BaseCursor__conn):
  37.             return None
  38.         
  39.         self._BaseCursor__conn = None
  40.  
  41.     
  42.     def _check_executed(self):
  43.         if not (self._executed):
  44.             raise ProgrammingError, 'execute() first'
  45.         
  46.  
  47.     
  48.     def setinputsizes(self, *args):
  49.         '''Does nothing, required by DB API.'''
  50.         pass
  51.  
  52.     
  53.     def setoutputsizes(self, *args):
  54.         '''Does nothing, required by DB API.'''
  55.         pass
  56.  
  57.     
  58.     def _get_db(self):
  59.         if not (self._BaseCursor__conn):
  60.             raise ProgrammingError, 'cursor closed'
  61.         
  62.         return self._BaseCursor__conn._db
  63.  
  64.     
  65.     def execute(self, query, args = None):
  66.         '''Execute a query.
  67.         
  68.         query -- string, query to execute on server
  69.         args -- optional sequence or mapping, parameters to use with query.
  70.         returns long integer rows affected, if any'''
  71.         ListType = ListType
  72.         TupleType = TupleType
  73.         import types
  74.         if args is None:
  75.             r = self._query(query)
  76.         elif type(args) is ListType and type(args[0]) is TupleType:
  77.             r = self.executemany(query, args)
  78.         else:
  79.             
  80.             try:
  81.                 r = self._query(query % self._BaseCursor__conn.literal(args))
  82.             except TypeError:
  83.                 m = None
  84.                 if m.args[0] in ('not enough arguments for format string', 'not all arguments converted'):
  85.                     raise ProgrammingError, m.args[0]
  86.                 else:
  87.                     raise 
  88.             except:
  89.                 m.args[0] in ('not enough arguments for format string', 'not all arguments converted')
  90.  
  91.         self._executed = query
  92.         return r
  93.  
  94.     
  95.     def executemany(self, query, args):
  96.         '''Execute a multi-row query.
  97.         
  98.         query -- string, query to execute on server
  99.         args -- sequence of sequences or mappings, parameters to use with
  100.             query. The query must contain the clause "values ( ... )".
  101.             The parenthetical portion will be repeated once for each
  102.             item in the sequence.
  103.         returns long integer rows affected, if any
  104.         
  105.         This method performs multiple-row inserts and similar queries.'''
  106.         join = join
  107.         import string
  108.         m = insert_values.search(query)
  109.         if not m:
  110.             raise ProgrammingError, "can't find values"
  111.         
  112.         p = m.start(1)
  113.         qv = query[p:]
  114.         qargs = self._BaseCursor__conn.literal(args)
  115.         
  116.         try:
  117.             q = [
  118.                 query % qargs[0]]
  119.             for a in qargs[1:]:
  120.                 q.append(qv % a)
  121.         except TypeError:
  122.             msg = None
  123.             if msg.args[0] in ('not enough arguments for format string', 'not all arguments converted'):
  124.                 raise ProgrammingError, msg.args[0]
  125.             else:
  126.                 raise 
  127.         except:
  128.             msg.args[0] in ('not enough arguments for format string', 'not all arguments converted')
  129.  
  130.         r = self._query(join(q, ',\n'))
  131.         self._executed = query
  132.         return r
  133.  
  134.     
  135.     def __do_query(self, q):
  136.         split = split
  137.         atoi = atoi
  138.         import string
  139.         db = self._get_db()
  140.         db.query(q)
  141.         self._result = self._get_result()
  142.         self.rowcount = db.affected_rows()
  143.         if not self._result and self._result.describe():
  144.             pass
  145.         self.description = None
  146.         self._insert_id = db.insert_id()
  147.         self._info = db.info()
  148.         self._check_for_warnings()
  149.         return self.rowcount
  150.  
  151.     
  152.     def _check_for_warnings(self):
  153.         pass
  154.  
  155.     _query = __do_query
  156.     
  157.     def info(self):
  158.         '''Return some information about the last query (db.info())'''
  159.         self._check_executed()
  160.         return self._info
  161.  
  162.     
  163.     def insert_id(self):
  164.         '''Return the last inserted ID on an AUTO_INCREMENT columns.'''
  165.         self._check_executed()
  166.         return self._insert_id
  167.  
  168.     
  169.     def _fetch_row(self, size = 1):
  170.         return self._result.fetch_row(size, self._fetch_type)
  171.  
  172.  
  173.  
  174. class CursorWarningMixIn:
  175.     '''This is a MixIn class that provides the capability of raising
  176.     the Warning exception when something went slightly wrong with your
  177.     query.'''
  178.     
  179.     def _check_for_warnings(self):
  180.         atoi = atoi
  181.         split = split
  182.         import string
  183.         if self._info:
  184.             warnings = atoi(split(self._info)[-1])
  185.             if warnings:
  186.                 raise Warning, self._info
  187.             
  188.         
  189.  
  190.  
  191.  
  192. class CursorStoreResultMixIn:
  193.     '''This is a MixIn class which causes the entire result set to be
  194.     stored on the client side, i.e. it uses mysql_store_result(). If the
  195.     result set can be very large, consider adding a LIMIT clause to your
  196.     query, or using CursorUseResultMixIn instead.'''
  197.     
  198.     def _get_result(self):
  199.         return self._get_db().store_result()
  200.  
  201.     
  202.     def close(self):
  203.         '''Close the cursor. Further queries will not be possible.'''
  204.         self._rows = ()
  205.         BaseCursor.close(self)
  206.  
  207.     
  208.     def _query(self, q):
  209.         rowcount = self._BaseCursor__do_query(q)
  210.         if not self._result and self._fetch_row(0):
  211.             pass
  212.         self._rows = ()
  213.         self._pos = 0
  214.         del self._result
  215.         return rowcount
  216.  
  217.     
  218.     def fetchone(self):
  219.         '''Fetches a single row from the cursor.'''
  220.         self._check_executed()
  221.         if self._pos >= len(self._rows):
  222.             return None
  223.         
  224.         result = self._rows[self._pos]
  225.         self._pos = self._pos + 1
  226.         return result
  227.  
  228.     
  229.     def fetchmany(self, size = None):
  230.         '''Fetch up to size rows from the cursor. Result set may be smaller
  231.         than size. If size is not defined, cursor.arraysize is used.'''
  232.         self._check_executed()
  233.         if not self._pos + size:
  234.             pass
  235.         end = self.arraysize
  236.         result = self._rows[self._pos:end]
  237.         self._pos = end
  238.         return result
  239.  
  240.     
  241.     def fetchall(self):
  242.         '''Fetchs all available rows from the cursor.'''
  243.         self._check_executed()
  244.         if not self._pos and self._rows[self._pos:]:
  245.             pass
  246.         result = self._rows
  247.         self._pos = len(self._rows)
  248.         return result
  249.  
  250.     
  251.     def seek(self, row, whence = 0):
  252.         '''seek to a given row of the result set analogously to file.seek().
  253.         This is non-standard extension.'''
  254.         self._check_executed()
  255.         if whence == 0:
  256.             self._pos = row
  257.         elif whence == 1:
  258.             self._pos = self._pos + row
  259.         elif whence == 2:
  260.             self._pos = len(self._rows) + row
  261.         
  262.  
  263.     
  264.     def tell(self):
  265.         '''Return the current position in the result set analogously to
  266.         file.tell(). This is a non-standard extension.'''
  267.         self._check_executed()
  268.         return self._pos
  269.  
  270.  
  271.  
  272. class CursorUseResultMixIn:
  273.     '''This is a MixIn class which causes the result set to be stored
  274.     in the server and sent row-by-row to client side, i.e. it uses
  275.     mysql_use_result(). You MUST retrieve the entire result set and
  276.     close() the cursor before additional queries can be peformed on
  277.     the connection.'''
  278.     
  279.     def close(self):
  280.         '''Close the cursor. No further queries can be executed.'''
  281.         self._result = None
  282.         BaseCursor.close(self)
  283.  
  284.     
  285.     def _get_result(self):
  286.         return self._get_db().use_result()
  287.  
  288.     
  289.     def fetchone(self):
  290.         '''Fetches a single row from the cursor.'''
  291.         self._check_executed()
  292.         r = self._fetch_row(1)
  293.         if r:
  294.             return r[0]
  295.         
  296.         return None
  297.  
  298.     
  299.     def fetchmany(self, size = None):
  300.         '''Fetch up to size rows from the cursor. Result set may be smaller
  301.         than size. If size is not defined, cursor.arraysize is used.'''
  302.         self._check_executed()
  303.         if not size:
  304.             pass
  305.         r = self._fetch_row(self.arraysize)
  306.         return r
  307.  
  308.     
  309.     def fetchall(self):
  310.         '''Fetchs all available rows from the cursor.'''
  311.         self._check_executed()
  312.         r = self._fetch_row(0)
  313.         return r
  314.  
  315.  
  316.  
  317. class CursorTupleRowsMixIn:
  318.     '''This is a MixIn class that causes all rows to be returned as tuples,
  319.     which is the standard form required by DB API.'''
  320.     _fetch_type = 0
  321.  
  322.  
  323. class CursorDictRowsMixIn:
  324.     '''This is a MixIn class that causes all rows to be returned as
  325.     dictionaries. This is a non-standard feature.'''
  326.     _fetch_type = 1
  327.     
  328.     def fetchoneDict(self):
  329.         '''Fetch a single row as a dictionary. Deprecated:
  330.         Use fetchone() instead.'''
  331.         return self.fetchone()
  332.  
  333.     
  334.     def fetchmanyDict(self, size = None):
  335.         '''Fetch several rows as a list of dictionaries. Deprecated:
  336.         Use fetchmany() instead.'''
  337.         return self.fetchmany(size)
  338.  
  339.     
  340.     def fetchallDict(self):
  341.         '''Fetch all available rows as a list of dictionaries. Deprecated:
  342.         Use fetchall() instead.'''
  343.         return self.fetchall()
  344.  
  345.  
  346.  
  347. class CursorOldDictRowsMixIn(CursorDictRowsMixIn):
  348.     """This is a MixIn class that returns rows as dictionaries with
  349.     the same key convention as the old Mysqldb (MySQLmodule). Don't
  350.     use this."""
  351.     _fetch_type = 2
  352.  
  353.  
  354. class CursorNW(CursorStoreResultMixIn, CursorTupleRowsMixIn, BaseCursor):
  355.     '''This is a basic Cursor class that returns rows as tuples and
  356.     stores the result set in the client. Warnings are not raised.'''
  357.     pass
  358.  
  359.  
  360. class Cursor(CursorWarningMixIn, CursorNW):
  361.     '''This is the standard Cursor class that returns rows as tuples
  362.     and stores the result set in the client. Warnings are raised as
  363.     necessary.'''
  364.     pass
  365.  
  366.  
  367. class DictCursorNW(CursorStoreResultMixIn, CursorDictRowsMixIn, BaseCursor):
  368.     '''This is a Cursor class that returns rows as dictionaries and
  369.     stores the result set in the client. Warnings are not raised.'''
  370.     pass
  371.  
  372.  
  373. class DictCursor(CursorWarningMixIn, DictCursorNW):
  374.     '''This is a Cursor class that returns rows as dictionaries and
  375.     stores the result set in the client. Warnings are raised as
  376.     necessary.'''
  377.     pass
  378.  
  379.  
  380. class SSCursorNW(CursorUseResultMixIn, CursorTupleRowsMixIn, BaseCursor):
  381.     '''This is a basic Cursor class that returns rows as tuples and
  382.     stores the result set in the server. Warnings are not raised.'''
  383.     pass
  384.  
  385.  
  386. class SSCursor(CursorWarningMixIn, SSCursorNW):
  387.     '''This is a Cursor class that returns rows as tuples and stores
  388.     the result set in the server. Warnings are raised as necessary.'''
  389.     pass
  390.  
  391.  
  392. class SSDictCursorNW(CursorUseResultMixIn, CursorDictRowsMixIn, BaseCursor):
  393.     '''This is a Cursor class that returns rows as dictionaries and
  394.     stores the result set in the server. Warnings are not raised.'''
  395.     pass
  396.  
  397.  
  398. class SSDictCursor(CursorWarningMixIn, SSDictCursorNW):
  399.     '''This is a Cursor class that returns rows as dictionaries and
  400.     stores the result set in the server. Warnings are raised as
  401.     necessary.'''
  402.     pass
  403.  
  404.